<?php

namespace Modules\Manager\Http\Controllers;

use App\Helpers\ResponseData;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Routing\Controller;
use Modules\Car\Entities\CarOrder;
use Modules\Manager\Entities\Department;

class OrderController extends Controller
{
    use ResponseData;
    //订单列表
   public function list(Request $request){
       $user = $request->user();
       $status = $request->input('status','');
       $process_status = $request->input('process_status','');
       $phone = $request->input('phone','');
       $department = $request->input('department','');
       $wx_name = $request->input('wx_name','');
       $source = $request->input('source','');

       if(!$request->filled('status')){
           $data1 = $this->getOrder($user,2,$process_status,$phone,$department,$wx_name,$source);
           $data2 = $this->getOther($user,$process_status,$phone,$department,$wx_name,$source);
           foreach ($data2 as $item){
               $data1[] = $item;
           }
           $data = $data1;

       }else{

           if(!is_array($status)){
               $status = json_decode($status,true);
           }

           if(in_array(2,$status)){
               $data1 = $this->getOrder($user,2,$process_status,$phone,$department,$wx_name,$source);
               foreach ($status as $k=>$v){
                   if($v==2){
                       unset($status[$k]);
                   }
               }

               if(count($status)>0){
                   $data2 = $this->getArrarOrder($user,$status,$process_status,$phone,$department,$wx_name,$source);
                   foreach ($data2 as $item){
                       $data1[] = $item;
                   }
                   $data = $data1;
               }else{
                   $data = $data1;
               }

           }else{
               $data = $this->getArrarOrder($user,$status,$process_status,$phone,$department,$wx_name,$source);
           }
       }
       $per_page = $request->input('size',10);
       $page = $request->input('page',0);
       $offset = ($page * $per_page) - $per_page;
       $total = count($data);
       $itme = $data->slice($offset, $per_page);
       $rooms = new LengthAwarePaginator(array_values($itme->toArray()), $total, $per_page, $page, ['path' => $request->url(), 'query' => $request->query()]);
       return $this->success($rooms);
   }

   public function getModel($process_status,$phone,$department,$wx_name,$source){
       return CarOrder::query()
           ->with(['user','expen','car','customer','gear'=>function($query){
               $query->select('id','plate_number');
           }])
           ->when($phone,function($query)use($phone){
               $query->where('phone',$phone);
           })
           ->when($department,function($query)use($department){
               $query->where('department',$department);
           })
           ->when($wx_name,function($query)use($wx_name){
               $query->where('wx_name','like','%'.$wx_name.'%');
           })
           ->when($process_status,function($query)use($process_status){
               $query->where('process_status',$process_status);
           })
           ->when($source,function($query)use($source){
               $query->where('source',$source);
           });
   }

    /**
     * 获取单个status 订单
     * @param $user
     * @param $status
     * @param $size
     * @param $process_status
     * @param $phone
     * @param $department
     * @param $wx_name
     * @param $source
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
   public function getOrder($user,$status,$process_status,$phone,$department,$wx_name,$source){
       $model = $this->getModel($process_status,$phone,$department,$wx_name,$source);
           if($status != 2){
               if(!$user->role){
                   $model->where('department',$user->department_id);
               }

           }
            $model->where('status',$status);
           return $model->orderByDesc('id')->get();
   }

    /**
     * 获取数组status 订单
     * @param $user
     * @param $status
     * @param $size
     * @param $process_status
     * @param $phone
     * @param $department
     * @param $wx_name
     */
   public function getArrarOrder($user,$status,$process_status,$phone,$department,$wx_name,$source){
       $model = $this->getModel($process_status,$phone,$department,$wx_name,$source);
       $res =  $model->whereIn('status',$status);
           if(!$user->role){
               $model = $res->where('department',$user->department_id);
           }

      return  $model->orderByDesc('id')->get();
   }
    public function getOther($user,$process_status,$phone,$department,$wx_name,$source){
        $model = $this->getModel($process_status,$phone,$department,$wx_name,$source);
        if(!$user->role){
            $model = $model->where('department',$user->department_id);
           }

        return $model->where('status','<>',2)
            ->orderByDesc('id')->get();
    }
   //订单详情
   public function edit(Request $request){
       $data = CarOrder::query()
           ->with(['user','customer'=>function($query){
               //$query->with('departments');
           },'car','gear','customer','expen'=>function($query){
               $query->select('order_sn','key','value');
           }])
           ->find($request->id);
       if($data->customer&&$data->customer->department_id){
           $data->departments = Department::query()
               ->select('id','name')
               ->find($data->customer->department_id);
       }
       return $this->success($data);
   }

    /**
     * 获取取送车列表
     */
   public function fetchOrReturnCar(Request $request){
       $size = $request->input('size',10);
       $data = CarOrder::query()
           ->with('car:id,name')
           ->select('fetch_time','verify_at','car_series_id','order_sn','license','send_employer_nickname','send_employer_phone','dispatch_at','fact_send_at','send_over_time','outset_photo','return_employer_nickname','return_employer_phone','overtime','return_photo')
           ->where('status','>',2)
           ->paginate($size);
       return $this->success($data);
   }

    /**
     * @param Request $request
     * @return mixed
     * 更新是否加班
     */
   public function updateOverTime(Request $request){
       $order = CarOrder::query()
           ->where('order_sn',$request->order_sn)
           ->first();
       if(!$order){
           return $this->failed('订单不存在',201);
       }else{
           if($request->filled('send_over_time')){
                $order->send_over_time = $request->send_over_time;
           }
           if($request->filled('overtime')){
               $order->overtime = $request->overtime;
           }
           $order->save();
           return $this->success('更新成功');
       }
   }
}
